package com.qf.hytrix.controller;

import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.qf.hytrix.service.*;
import com.qf.service.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @Auther: Song
 * @Date: 2022-03-18 17:01
 */
@RestController
public class HelloController {
    @Autowired
    HelloService helloService;
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    BookService bookService;
    @Autowired
    BookService2 bookService2;

    @GetMapping("/test02")
    public void test02() throws ExecutionException, InterruptedException {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        BookCollapseCommand command1 = new BookCollapseCommand(100, bookService2);
        BookCollapseCommand command2 = new BookCollapseCommand(101, bookService2);
        BookCollapseCommand command3 = new BookCollapseCommand(102, bookService2);

        Future<Book> f1 = command1.queue();
        Future<Book> f2 = command2.queue();
        Future<Book> f3 = command3.queue();

        Book b1 = f1.get();
        Book b2 = f2.get();
        Book b3 = f3.get();

        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("b3 = " + b3);

        Thread.sleep(1000);

        BookCollapseCommand command = new BookCollapseCommand(103, bookService2);
        Future<Book> queue = command.queue();
        Book book = queue.get();
        System.out.println("book = " + book);

        context.close();
    }

    @GetMapping("/test01")
    public void test01() {
        //缓存有一个范围
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        //初始化上下文之后，缓存就开始了
        Book b1 = bookService.getBookById(99,"");
        Book b2 = bookService.getBookById(99,"");
        Book b3 = bookService.getBookById(99,"");
        //到上下文关闭，缓存结束
        context.close();
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("b3 = " + b3);
    }

    @GetMapping("/hello")
    public String hello() {
//        return helloService.hello();

        HelloCommand command = new HelloCommand(restTemplate);

        String execute = command.execute();

        return execute;
    }

    @GetMapping("/hello2")
    public String hello2() {
        HystrixRequestContext context = HystrixRequestContext.initializeContext();
        HelloCommand2 command2 = new HelloCommand2(restTemplate, 88);

        String s = command2.execute();
        HelloCommand2 command21 = new HelloCommand2(restTemplate, 88);
        try {
            Future<String> future = command21.queue();
            String s1 = future.get();
            System.out.println("s = " + s);
            System.out.println("s1 = " + s1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        context.close();
        return s;
    }
}
